package demo.components.kafka.demo;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

import demo.components.kafka.KafkaClient;
import demo.components.kafka.KafkaConfig;

public class KafkaTest {

    private static final Logger logger = Logger.getLogger(KafkaTest.class);
    static{
        String clientId="nlb_ddb";
        String brokers = "10.180.156.21:9092";
        String topic = "napm.comb.nonagent";
        int batchSize = 1;
        int retry = 3;
        boolean ack = true;
        KafkaConfig kafkaConfig = new KafkaConfig();
        kafkaConfig.setTopic(topic);
        kafkaConfig.setClientId(clientId);
        kafkaConfig.setBrokers(brokers);
        kafkaConfig.setBatchSize(batchSize);
        kafkaConfig.setRetry(retry);
        kafkaConfig.setAck(ack);
        try {
            KafkaClient.initOrReset("test", kafkaConfig);
        } catch (IOException e) {
            logger.error("kafka init or reset error", e);
        }
    }

    public static void main(String[] args) {
        String string = mockData();
        System.out.println("string: " + string);
        for (int i=1; i<1000; i++) {
            send(string);
        }
    }

    private static void send(String str) {
        try {
            KafkaClient.send(str, "test");
        }
        catch (Exception e) {
            logger.error("send kafka fail,cause:" + str, e);
        }
    }

    public static String mockData() {
        JSONObject obj = new JSONObject();
        obj.put("node", "aa");
        obj.put("productId", "mockncrprojectid");
        obj.put("provider", "NCR");
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("mininum", 2);
        map1.put("maxinum", 6);
        map1.put("sum", 50);
        map1.put("createTime", System.currentTimeMillis());
        map1.put("sampleCount", 2);
        map1.put("average", 5);
        map1.put("metricName", "cpuUsage");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("mininum", 3);
        map2.put("maxinum", 8);
        map2.put("sum", 40);
        map2.put("createTime", System.currentTimeMillis());
        map2.put("sampleCount", 3);
        map2.put("average", 4);
        map2.put("metricName", "b");
        list.add(map1);
        list.add(map2);
        obj.put("metricDatas", list);
        obj.put("resource", "disk=aa");
        //obj.put("namespace", "*");
        //obj.put("service", "*");

        String string = obj.toJSONString();
        return string;
    }

}
